<?php

require_once 'C:\wamp\www\projet_symfony\app_clinique\lib\Zend\Loader.php';

class planningActions extends sfActions
{

  public function executeIndex()
  {
    $this->forward('default', 'module');
  }

  public function executePlanning()
  {

    Zend_Loader::loadClass('Zend_Gdata');
    Zend_Loader::loadClass('Zend_Gdata_AuthSub'); 
    Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
    Zend_Loader::loadClass('Zend_Gdata_Calendar');
    Zend_Loader::loadClass('Zend_Http_Client');
    Zend_Loader::loadClass('Zend_Http_Client_Adapter_Proxy');
    

    $config = array(
            'adapter'    => 'Zend_Http_Client_Adapter_Proxy',
            'proxy_host' => 'proxy.etudiant.insia.org',
            'proxy_port' => 3128
        );

    $httpproxy = new Zend_Http_Client("http://www.google.com:443", $config);


    // create authenticated HTTP client for Calendar service
    $gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    $user = "cliniqueinsia@gmail.com";
    $pass = "cliniqueinsia2009";
  //  $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);

     $httpproxy = new Zend_Http_Client("http://www.google.com:443", $config);
     $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cp',$httpproxy);
     $client->setConfig($config);


    //$this->forward('default', 'module');
  }

  public function executeAddEvent()
  {
      Zend_Loader::loadClass('Zend_Gdata');
      Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
      Zend_Loader::loadClass('Zend_Gdata_Calendar');
      Zend_Loader::loadClass('Zend_Http_Client');
      if($this->getRequest()->getMethod() == sfRequest::POST)
      {

          // Paramètres de configuration
        $config = array(
            'adapter'    => 'Zend_Http_Client_Adapter_Proxy',
            'proxy_host' => 'proxy.etudiant.insia.org',
            'proxy_port' => 3128
        );



          // connect to service
          $gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
          $user = "cliniqueinsia@gmail.com";
          $pass = "cliniqueinsia2009";
          $httpproxy = new Zend_Http_Client("http://www.google.com:443", $config);
          $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cl',$httpproxy);
          $client->setConfig($config);
          $gcal = new Zend_Gdata_Calendar($client);

          $title = $_POST['title'];
          $start = date(DATE_ATOM, mktime($_POST['sdate_hh'] - 2, $_POST['sdate_ii'],
           0, $_POST['sdate_mm'], $_POST['sdate_dd'], $_POST['sdate_yy']));
          $end = date(DATE_ATOM, mktime($_POST['edate_hh'] - 2, $_POST['edate_ii'],
           0, $_POST['edate_mm'], $_POST['edate_dd'], $_POST['edate_yy']));
		   
		   /*$this->widgetSchema['date'] = new sfWidgetFormJQueryDate(
				array(
				'config' => '{}',
				'culture' => 'fr',
				'format' => '%day%/%month%/%year%'
				)
			);
            */
          // construct event object
          // save to server
          try
          {
            $event = $gcal->newEventEntry();
            $event->title = $gcal->newTitle($title);
            $when = $gcal->newWhen();
            $when->startTime = $start;
            $when->endTime = $end;
            $event->when = array($when);
            $gcal->insertEvent($event);
            return $this->redirect("planning/planning");
            
          }
          catch (Zend_Gdata_App_Exception $e)
          {
            echo "Error: " . $e->getResponse();
          }
      }
  }

  public function executeDeleteEvent()
  {
    Zend_Loader::loadClass('Zend_Gdata');
    Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
    Zend_Loader::loadClass('Zend_Gdata_Calendar');
    Zend_Loader::loadClass('Zend_Http_Client');

    // connect to service
    $gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    $user = "cliniqueinsia@gmail.com";
    $pass = "cliniqueinsia2009";
    $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
    $this->gcal = new Zend_Gdata_Calendar($client);

    // retrieve event
    $this->query = $this->gcal->newEventQuery();
    $this->query->setUser('default');
    $this->query->setVisibility('private');
    $this->query->setProjection('basic');
    if(isset($_GET['q']))
        $this->query->setQuery($_GET['q']);

    // delete event
    if($this->getRequest()->getMethod() == sfRequest::GET && $this->getRequest()->hasParameter('eventid'))
    {
        try
        {
          $event = $this->gcal->getCalendarEventEntry("http://www.google.com/calendar/feeds/default/private/full/".$this->getRequestParameter('eventid'));
          $event->delete();
          return $this->redirect("planning/planning");
        }
        catch (Zend_Gdata_App_Exception $e)
        {
          echo "Error: " . $e->getResponse();
        }
    }

  }

    public function executeEditEvent()
    {

        Zend_Loader::loadClass('Zend_Gdata');
        Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
        Zend_Loader::loadClass('Zend_Gdata_Calendar');
        Zend_Loader::loadClass('Zend_Http_Client');

       // connect to service
        $gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
        $user = "cliniqueinsia@gmail.com";
        $pass = "cliniqueinsia2009";
        $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
        $this->gcal = new Zend_Gdata_Calendar($client);

        // generate query to get event list
        $this->query = $this->gcal->newEventQuery();
        $this->query->setUser('default');
        $this->query->setVisibility('private');
        $this->query->setProjection('basic');
        if(isset($_GET['q']))
             $this->query->setQuery($_GET['q']);

    }

    public function executeEditEventForm()
    {
        Zend_Loader::loadClass('Zend_Gdata');
        Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
        Zend_Loader::loadClass('Zend_Gdata_Calendar');
        Zend_Loader::loadClass('Zend_Http_Client');

       // connect to service
        $gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
        $user = "cliniqueinsia@gmail.com";
        $pass = "cliniqueinsia2009";
        $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
        $this->gcal = new Zend_Gdata_Calendar($client);

        // generate query to get event list
        $this->query = $this->gcal->newEventQuery();
        $this->query->setUser('default');
        $this->query->setVisibility('private');
        $this->query->setProjection('basic');

        if($this->getRequest()->getMethod() == sfRequest::GET && $this->getRequest()->hasParameter('eventid'))
        {
            // set new event properties and update event
            try
            {
                $this->eventid = $this->getRequestParameter('eventid');
                $event = $this->gcal->getCalendarEventEntry("http://www.google.com/calendar/feeds/default/private/full/".$this->getRequestParameter('eventid'));
                $this->titre = $event->title;
                $when = $event->getWhen();
                $startTime = strtotime($when[0]->getStartTime());
                $this->sdate_dd = date('d', $startTime);
                $this->sdate_mm = date('m', $startTime);
                $this->sdate_yy = date('Y', $startTime);
                $this->sdate_hh = date('H', $startTime) + 2;
                $this->sdate_ii = date('i', $startTime);
                $endTime = strtotime($when[0]->getEndTime());
                $this->edate_dd = date('d', $endTime);
                $this->edate_mm = date('m', $endTime);
                $this->edate_yy = date('Y', $endTime);
                $this->edate_hh = date('H', $endTime) + 2;
                $this->edate_ii = date('i', $endTime);
            }
            catch (Zend_Gdata_App_Exception $e)
            {
              die("Error: " . $e->getResponse());
            }
        }
        else
        {
            if($this->getRequest()->getMethod() == sfRequest::POST)
            {
                $title = $_POST['title'];
                $start = date(DATE_ATOM, mktime($_POST['sdate_hh'] - 2, $_POST['sdate_ii'],0, $_POST['sdate_mm'], $_POST['sdate_dd'], $_POST['sdate_yy']));
                $end = date(DATE_ATOM, mktime($_POST['edate_hh'] - 2, $_POST['edate_ii'],0, $_POST['edate_mm'], $_POST['edate_dd'], $_POST['edate_yy']));

                // set new event properties and update event
                try
                {
                  $event = $this->gcal->getCalendarEventEntry("http://www.google.com/calendar/feeds/default/private/full/".$_POST['eventid']);
                  $event->title = $this->gcal->newTitle($title);
                  $when = $this->gcal->newWhen();
                  $when->startTime = $start;
                  $when->endTime = $end;
                  $event->when = array($when);
                  $event->save();
                  return $this->redirect("planning/planning");

                }
                catch (Zend_Gdata_App_Exception $e)
                {
                  die("Error: " . $e->getResponse());
                }
            }

        }
    }

}
